package com.sy.core.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.core.model.SyBanner;
import com.sy.core.model.SyOrderInfo;
import com.sy.core.model.SyOrderInfoDetail;
import com.sy.core.util.RestResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderCtl extends BaseCtl{
    private static final String prefix = "/back/order/";
    @RequestMapping("index")
    public String index(){
        return prefix+"index";
    }

    @RequestMapping("list")
    @ResponseBody
    public RestResult list(Integer page, Integer limit, SyOrderInfo syOrderInfo){
        RestResult rr =null;
        PageHelper.startPage(page,limit);
        try {
            List<Map<String, Object>> orderList = orderService.queryList(syOrderInfo);
            PageInfo<Map<String,Object>> info = new PageInfo<>(orderList);
            rr = new RestResult("200","获取列表数据成功",info);
        } catch (Exception e) {
            logger.error("获取列表数据失败");
            rr = new RestResult("500","获取列表数据失败");
        }
        return  rr;
    }

    @RequestMapping("edit")
    public String update(String pk,ModelMap mm){
        Map<String, Object> map = orderService.queryOne(pk);
        //h获取一个订单中的多个产品或sku
        SyOrderInfoDetail syOrderInfoDetail = new SyOrderInfoDetail();
        syOrderInfoDetail.setOrderPk(pk);
        List<Map<String, Object>> orderInfoDetailList = orderInfoDetailService.queryList(syOrderInfoDetail);
        String productList= JSONObject.toJSONString(orderInfoDetailList);
        mm.addAttribute("productList",productList);
        mm.addAttribute("mv",map);
        return prefix+"orderEdit";
    }

    /**
     * table列表，多个产品或者sku详情
     */
    @RequestMapping("skuList")
    @ResponseBody
    public  RestResult skuList(SyOrderInfoDetail syOrderInfoDetail,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Map<String, Object>> orderInfoDetailList = orderInfoDetailService.queryList(syOrderInfoDetail);
        PageInfo<Map<String,Object>> info = new PageInfo<>(orderInfoDetailList);
        return new RestResult("200","查询成功",info);
    }

    @RequestMapping("update")
    @ResponseBody
    public RestResult save(SyOrderInfo syOrderInfo){
        RestResult rr = null;
        if (syOrderInfo.getPk()!=null){
            orderService.update(syOrderInfo);
        }
        return rr;
    }

    @RequestMapping("del")
    @ResponseBody
    public RestResult del(String pks){
        RestResult rr = null;
        try {
            bannerService.batchDel(pks);
            rr = new RestResult("200","删除成功");
        } catch (Exception e) {
            logger.error("删除失败");
            rr = new RestResult("500","删除失败");
        }
        return rr;
    }
}
